package com.nexen.merchant.statistics.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.nexen.common.annotation.Anonymous;
import com.nexen.common.core.controller.BaseController;
import com.nexen.common.core.domain.AjaxResult;
import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.core.domain.model.LoginUser;
import com.nexen.common.utils.DateUtils;
import com.nexen.common.utils.SecurityUtils;
import com.nexen.common.utils.StringUtils;
import com.nexen.merchant.statistics.domain.StatisticsLable;
import com.nexen.merchant.statistics.domain.StatisticsRequest;
import com.nexen.merchant.statistics.service.INexenStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>Title: StatisticsController</p>
 * <p>Description: </p>
 *
 * @author cong.zhang
 * @version 1.0.0
 * @date 2024/8/9
 * @time 1:15
 */
@RestController
@RequestMapping("/statistics")
@Anonymous
public class NexenStatisticsController extends BaseController {

    @Autowired
    private INexenStatisticsService nexenStatisticsService;

    private static String weekStartMonthTime = LocalDateTimeUtil.format(DateUtils.last30DaysStartTime(),"yyyy-MM-dd");
    private static String weekEndMonthTime = LocalDateTimeUtil.format(DateUtils.last30DaysEndTime(),"yyyy-MM-dd");

    @GetMapping("/member/count")
    public AjaxResult memberCount(HttpServletRequest request, HttpServletResponse response) {
        Long result = nexenStatisticsService.memberCount();
        return AjaxResult.success(result);
    }
    @PostMapping("/member/week")
    public AjaxResult memberWeek(@RequestBody StatisticsRequest statisticsRequest) {
        List<StatisticsLable> result = null;
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        )  {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        result = nexenStatisticsService.memberWeek2(statisticsRequest);
        return AjaxResult.success(result);
    }

    /**
     *
     * @param statisticsRequest
     * @return
     */
    @PostMapping("/warrantycard/week")
    public AjaxResult warrantycardWeek(@RequestBody StatisticsRequest statisticsRequest) {
        List<StatisticsLable> result = null;
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        )  {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                List<String> cityListNew = statisticsRequest.getCityList();
                String cityIds = currentUser.getCityIds();
                List<String> cityListOld = Arrays.asList(cityIds.split(";"));
                // 登录人员省份数据是null 默认赋值绑定的省份
                if (StringUtils.isNull(cityListNew)){
                    if (cityListOld != null && cityListOld.size() > 0) {
                        statisticsRequest.setCityOldList(cityListOld);
                    }
                } else {
                    // 登录人员省份数据不为空 处理他选中的省份和他绑定的省份进行比对，没有的剔除掉
                    statisticsRequest.setCityList(cityListNew);
                    statisticsRequest.setCityOldList(cityListOld);
                }
            }
        }

        result = nexenStatisticsService.warrantyCardWeek2(statisticsRequest);
        return AjaxResult.success(result);
    }

    /**
     *
     * @param statisticsRequest
     * @return
     */
    @PostMapping("/servicerequest/week")
    public AjaxResult servicerequestWeek(@RequestBody StatisticsRequest statisticsRequest) {
        List<StatisticsLable> result = null;
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        )  {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                List<String> cityListNew = statisticsRequest.getCityList();
                String cityIds = currentUser.getCityIds();
                List<String> cityListOld = Arrays.asList(cityIds.split(";"));
                // 登录人员省份数据是null 默认赋值绑定的省份
                if (StringUtils.isNull(cityListNew)){
                    if (cityListOld != null && cityListOld.size() > 0) {
                        statisticsRequest.setCityOldList(cityListOld);
                    }
                } else {
                    // 登录人员省份数据不为空 处理他选中的省份和他绑定的省份进行比对，没有的剔除掉
                    statisticsRequest.setCityList(cityListNew);
                    statisticsRequest.setCityOldList(cityListOld);
                }
            }
        }

        result = nexenStatisticsService.serviceRequestWeek2(statisticsRequest);
        return AjaxResult.success(result);
    }

    @PostMapping("/warrantycard/status")
    public AjaxResult warrantycardStatus(@RequestBody StatisticsRequest statisticsRequest) {
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        )  {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                List<String> cityListNew = statisticsRequest.getCityList();
                String cityIds = currentUser.getCityIds();
                List<String> cityListOld = Arrays.asList(cityIds.split(";"));
                // 登录人员省份数据是null 默认赋值绑定的省份
                if (StringUtils.isNull(cityListNew)){
                    if (cityListOld != null && cityListOld.size() > 0) {
                        statisticsRequest.setCityOldList(cityListOld);
                    }
                } else {
                    // 登录人员省份数据不为空 处理他选中的省份和他绑定的省份进行比对，没有的剔除掉
                    statisticsRequest.setCityList(cityListNew);
                    statisticsRequest.setCityOldList(cityListOld);
                }
            }
        }

        List<StatisticsLable> result = nexenStatisticsService.warrantyCardStatus(statisticsRequest);
        return AjaxResult.success(result);
    }

    @PostMapping("/servicerequest/status")
    public AjaxResult servicerequestStatus(@RequestBody StatisticsRequest statisticsRequest) {
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        )  {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                List<String> cityListNew = statisticsRequest.getCityList();
                String cityIds = currentUser.getCityIds();
                List<String> cityListOld = Arrays.asList(cityIds.split(";"));
                // 登录人员省份数据是null 默认赋值绑定的省份
                if (StringUtils.isNull(cityListNew)){
                    if (cityListOld != null && cityListOld.size() > 0) {
                        statisticsRequest.setCityOldList(cityListOld);
                    }
                } else {
                    // 登录人员省份数据不为空 处理他选中的省份和他绑定的省份进行比对，没有的剔除掉
                    statisticsRequest.setCityList(cityListNew);
                    statisticsRequest.setCityOldList(cityListOld);
                }
            }
        }

        List<StatisticsLable> result = nexenStatisticsService.serviceRequestStatus(statisticsRequest);
        return AjaxResult.success(result);
    }

    @PostMapping("/warrantycard/province/count")
    public AjaxResult warrantycardProvinceCount(@RequestBody StatisticsRequest statisticsRequest) {
//        if (StrUtil.isBlank(statisticsRequest.getBeginTime()) ||
//                StrUtil.isBlank(statisticsRequest.getEndTime())
//        ) {
//            statisticsRequest.setBeginTime(weekStartMonthTime);
//            statisticsRequest.setEndTime(weekEndMonthTime);
//        }
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                List<String> cityListNew = statisticsRequest.getCityList();
                String cityIds = currentUser.getCityIds();
                List<String> cityListOld = Arrays.asList(cityIds.split(";"));
                // 登录人员省份数据是null 默认赋值绑定的省份
                if (StringUtils.isNull(cityListNew)){
                    if (cityListOld != null && cityListOld.size() > 0) {
                        statisticsRequest.setCityOldList(cityListOld);
                    }
                } else {
                    // 登录人员省份数据不为空 处理他选中的省份和他绑定的省份进行比对，没有的剔除掉
                    statisticsRequest.setCityList(cityListNew);
                    statisticsRequest.setCityOldList(cityListOld);
                }
            }
        }

        List<StatisticsLable> result = nexenStatisticsService.warrantycardProvinceCount(statisticsRequest);
        return AjaxResult.success(result);
    }

}
